import axios from 'axios'

const instance = axios.create({
    baseURL: 'http://tynewadmin.com/index.php/admin',
    // baseURL: 'http://cc.sxqhkj.com/admin'
})

instance.interceptors.request.use(
    config => {
        let token = sessionStorage.getItem('token')
        if (token) {
            config.headers["Authorization"] = token
        }
        return config
    }, error => {
        return Promise.reject(error)
    })

instance.interceptors.response.use(
    response => {
        if (response.data.code == 1000) {
            sessionStorage.removeItem('token')
            response.sendRedirect("/")
        }
        return response
    }, error => {
        return Promise.reject(error)
    })

export function get(url, params) {
    return instance.get(url, {
        params
    })
}

export function post(url, params) {
    return instance.post(url, params)
}

export function del(url, params) {
    return instance.delete(url, {
        params
    })
}

export function put(url, params) {
    return instance.put(url, params)
}